<template>
    <div class="app-container calendar-list-container">

        <!-- 查询和其他操作 -->
        <div class="filter-container">
          <label>订单状态：</label>
          <el-select v-model="listQuery.orderStatus" placeholder="订单状态">
            <el-option
              v-for="item in orderStatusSelect"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <label>支付状态：</label>
          <el-select v-model="listQuery.payStatus" placeholder="支付状态">
            <el-option
              v-for="item in payStatusSelect"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <label>类型：</label>
          <el-select v-model="listQuery.type" placeholder="类型">
            <el-option
              v-for="item in typeSelect"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <label>指定预测人：</label>
          <el-select v-model="listQuery.targetTea" placeholder="指定预测人">
            <el-option key="" lable="请选择" value=""></el-option>
            <el-option
              v-for="item in recommendList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId">
            </el-option>
          </el-select>
          <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查找</el-button>
        </div>

        <!-- 查询结果 -->
        <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>
          <el-table-column align="center" min-width="150px" label="平台编号" prop="platformId">
          </el-table-column>   
          <el-table-column align="center" min-width="150px" label="类型" prop="type">
            <template slot-scope="scope">
              <span v-if="scope.row.type == 1">一事一测</span>
              <span v-if="scope.row.type == 2">一年运势预测</span>
              <span v-if="scope.row.type == 3">八字简批</span>
              <span v-if="scope.row.type == 4">个人起名改名</span>
              <span v-if="scope.row.type == 5">单位企业起名</span>
              <span v-if="scope.row.type == 6">商标命名</span>
              <span v-if="scope.row.type == 7">开业择日</span>
              <span v-if="scope.row.type == 8">大型户外活动择日，策划</span>
              <span v-if="scope.row.type == 9">结婚择日</span>
              <span v-if="scope.row.type == 10">八字合婚</span>
              <span v-if="scope.row.type == 11">免费风水咨询</span>
            </template>
          </el-table-column>
          <el-table-column align="center" min-width="200px" label="实际付款金额" prop="amt">
          </el-table-column>  
          <el-table-column align="center" min-width="200px" label="预测内容" prop="content">
          </el-table-column>       
          <el-table-column align="center" min-width="100px" label="提交时间" prop="activityStartTime">
            <template slot-scope="scope">
              <span>{{scope.row.createTime | formatDate1}}</span>
            </template>
          </el-table-column>   
          <el-table-column align="center" min-width="200px" label="支付状态" prop="payStatus">
              <template slot-scope="scope">
                  <span v-if="scope.row.payStatus == 1">已付款</span>
                  <span v-if="scope.row.payStatus == 2">未付款</span>
                  <span v-if="scope.row.payStatus == 3">付款失败</span>
                  <span v-if="scope.row.payStatus == 4">已退款</span>
                  <span v-if="scope.row.payStatus == 5">退款失败</span>
              </template>
          </el-table-column>   
          <el-table-column align="center" min-width="100px" label="订单状态" prop="orderStatus">
             <template slot-scope="scope">
              <span v-if="scope.row.orderStatus == 1">待回复</span>
              <span v-if="scope.row.orderStatus == 2">已回复</span>
              <span v-if="scope.row.orderStatus == 3">已完成</span>
            </template>
          </el-table-column>
          <el-table-column align="center" min-width="200px" label="指定回复人" prop="teaName">
          </el-table-column> 
          <el-table-column align="center" label="操作" width="150" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button  type="primary" size="mini" v-if="loveCalculationRecordManager_info && scope.row.orderStatus == 3"  @click="handleInfo(scope.row)">详情</el-button>
                <el-button  type="primary" size="mini" v-if="loveCalculationRecordManager_info && scope.row.orderStatus != 3"  @click="apply(scope.row)">回复</el-button>
                <el-button  type="primary" size="mini"  @click="complete(scope.row)">完成</el-button>
              </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
            :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        </div>

        <!-- 添加对话框 -->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
          <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="100px" style='width: 400px; margin-left:50px;'>
            <el-form-item label="咨询内容" prop="content">
              <div class="content_div" style="width:300px;height:200px;border: 1px solid #e3e3e3" clearable="">{{content}}</div>
            </el-form-item>
            <el-form-item label="沟通内容">
              <div class="content_div" style="width:300px;border: 1px solid #e3e3e3" clearable="" v-for="item in applyList" :key="item">
                <span v-if="item.applyUserId == null">
                  问：
                </span>
                <span v-if="item.applyUserId != null">
                  回复：
                </span>
                <span>{{item.content}}</span>
                </div>
            </el-form-item> 
            <el-form-item label="回复内容" prop="content">
              <el-input v-model="dataForm.content" type="textarea" :rows="8" clearable=""></el-input>
            </el-form-item>           
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button v-if="dialogStatus == 'apply'" type="primary" @click="updateData('dataForm')">确定</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
import waves from '@/directive/waves' // 水波纹指令
import { queryListPage, queryInfo, apply, queryReplyRecord, complete } from '@/api/cms/loveCalculation/record'
import { page } from '@/api/cms/loveCalculation/recommend'
import { mapGetters } from 'vuex'
import { formatDate } from '@/utils/index'

export default {
  name: 'activity',
  directives: {
    waves
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        payStatus: '',
        orderStatus: '',
        type: '',
        startTime: '',
        endTime: '',
        platformId: '',
        merchantId: '',
        targetTea: ''
      },
      list: [],
      content: '',
      total: 0,
      listLoading: false,
      dialogFormVisible: false,
      downloadLoading: false,
      recommendList: [],
      loveCalculationRecordManager_info: false,
      typeSelect: [
        {
          value: '',
          label: '请选择'
        }, {
          value: 1,
          label: '一事一测'
        }, {
          value: 2,
          label: '一年运势预测'
        }, {
          value: 3,
          label: '八字简批'
        }, {
          value: 4,
          label: '个人起名改名'
        }, {
          value: 5,
          label: '单位企业起名'
        }, {
          value: 6,
          label: '商标命名'
        }, {
          value: 7,
          label: '开业择日'
        }, {
          value: 8,
          label: '大型户外活动择日，策划'
        }, {
          value: 9,
          label: '结婚择日'
        }, {
          value: 10,
          label: '八字合婚'
        }, {
          value: 11,
          label: '免费风水咨询'
        }, {
          value: 12,
          label: '八字排盘'
        }, {
          value: 13,
          label: '六爻起卦'
        }
      ],
      orderStatusSelect: [
        {
          value: '',
          label: '请选择'
        }, {
          value: 1,
          label: '待回复'
        }, {
          value: 2,
          label: '已回复'
        }, {
          value: 3,
          label: '已完成'
        }],
      payStatusSelect: [
        {
          value: '',
          label: '请选择'
        }, {
          value: 1,
          label: '已付款'
        }, {
          value: 2,
          label: '未付款'
        }, {
          value: 3,
          label: '付款失败'
        }, {
          value: 4,
          label: '已退款'
        }, {
          value: 5,
          label: '退款失败'
        }],
      dataForm: {
        platformId: '',
        merchantId: '',
        id: '',
        applyUserId: '',
        content: '',
        type: '',
        topId: ''
      },
      applyList: [],
      textMap: {
        create: '创建',
        apply: '回复'
      },
      dialogStatus: '',
      rules: {
      },
      pickerStartDateBegin: {
        disabledDate: (time) => {
          return time.getTime() < (Date.now() - 24 * 60 * 60 * 1000)
        }
      },
      pickerEndDateAfter: {
        disabledDate: (time) => {
          return time.getTime() < this.dataForm.activityStartTime
        }
      }
    }
  },
  filters: {
    formatDate1(time) {
      var date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm')
    }
  },
  created() {
    this.getList()
    this.queryRecommend()
    this.loveCalculationRecordManager_info = this.elements['loveCalculationRecordManager:info']
  },
  computed: {
    ...mapGetters([
      'elements',
      'name',
      'userId'
    ])
  },

  methods: {
    apply(row) {
      this.dialogFormVisible = true
      this.dialogStatus = 'apply'
      this.content = row.content
      this.dataForm.type = row.type
      this.dataForm.topId = row.id
      var obj = { 'id': row.id }
      queryReplyRecord(obj).then(response => {
        console.log('=====--------5555555555-----------------====')
        console.log(response)
        this.applyList = response.data
        if (this.applyList.length === 0) {
          this.dataForm.parentId = row.id
        } else {
          this.dataForm.parentId = this.applyList[this.applyList.length - 1].parentId
        }
      }).catch(() => {
      })
    },
    queryRecommend() {
      var listQueryRecommend = { page: 1, limit: 10, platformId: this.platformId }
      page(listQueryRecommend).then(response => {
        console.log('查询出来的全部推荐人')
        console.log(response.data.rows)
        this.recommendList = response.data.rows
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    complete(row) {
      var obj = { 'id': row.id }
      complete(obj).then(response => {
        console.log('=====--------5555555555-----------------====')
        console.log(response)
        this.$notify({
          title: '成功',
          message: '咨询结束',
          type: 'success',
          duration: 2000
        })
        this.getList()
      }).catch(() => {
      })
    },
    handleInfo(row) {
      this.dialogFormVisible = true
      var obj = { id: row.id }
      queryInfo(obj).then(response => {
        console.log('=====--------4444444-----------------====')
        console.log(response)
        this.dataForm.reply = response.data.reply
        this.content = response.data.content
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
      this.getList()
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    getList() {
      this.listLoading = true
      this.listQuery.platformId = this.platformId
      queryListPage(this.listQuery).then(response => {
        console.log('=====--------3333333333-----------------====')
        console.log(response)
        this.list = response.data.data.rows
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleUpdate(row) {
    },
    updateData(formName) {
      this.dataForm.applyUserId = this.userId
      console.log('ppppppppppppppppppppppppppppppppppppppp')
      console.log(this.dataForm)
      apply(this.dataForm).then(response => {
        console.log('=====--------4444444-----------------====')
        console.log(response)
        this.getList()
        this.$notify({
          title: '成功',
          message: '回复成功',
          type: 'success',
          duration: 2000
        })
        this.resetForm()
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleDelete(row) {
    },
    handleCreate() {
      this.resetForm()
    },
    createData(formName) {
    },
    handleDetail(row) {

    },
    handleClose(row) {
      this.$confirm('此操作将结束选中活动, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
        })
    },
    resetForm() {
      this.dialogFormVisible = false
    }
  }
}
</script>

<style>

</style>


